package com.hundredapp.oa.vo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.domain.OaContract;
import com.hundredapp.oa.domain.OaElect;
import com.hundredapp.oa.repository.ContractManager;
import com.hundredapp.oa.repository.ElectManager;
import com.hundredapp.oa.repository.SubjectManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.util.WebUtil;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class PrintContract extends ActionSupport {
	@Autowired
	private ContractManager contractManager;
	@Autowired
	private ElectManager electManager;

	@Autowired
	private SubjectManager subjectManager;
	
	@Autowired
	private UserManager userManager;

	private List<OaElect> elects = new ArrayList<OaElect>(0);
	
	private List<OaElect> pelects = new ArrayList<OaElect>(0);

	private OaElect presentation;
	private String parent;

	private String startDate;

	private OaContract oaContract;

	public String execute() {
		Double payables = 0.0;
		Double disbursements = 0.0;

		oaContract = new OaContract();
		oaContract.setState(1);
		oaContract.setCreateUser(userManager.findUserById(WebUtil.GetLoginUserId()));
		Integer cid = contractManager.add(oaContract);
		OaContract c = contractManager.findById(cid);
		List<OaElect> es = new ArrayList<OaElect>(0);
		List<OaElect> pes = new ArrayList<OaElect>(0);
		
		for(OaElect ele:pelects) {
			if (ele == null)
				continue;
			ele.setOaContract(c);
			ele.setOaSubject(subjectManager.findById(ele.getOaSubject()
					.getId()));
			ele.setIspre(0);
			pes.add(ele);
		}
		
		for (OaElect each : elects) {
			if (each == null)
				continue;
			each.setOaContract(c);
			each.setOaSubject(subjectManager.findById(each.getOaSubject()
					.getId()));
			payables += each.getOaSubject().getPrice();
			disbursements += each.getPrice();
			each.setIspre(1);
			es.add(each);
		}
		oaContract.setElects(es);
		oaContract.setPayables(payables);
		oaContract.setDisbursements(disbursements);
		contractManager.save(oaContract);

		elects = es;
		pelects = pes;
		return SUCCESS;
	}

	public void setContractManager(ContractManager contractManager) {
		this.contractManager = contractManager;
	}

	public List<OaElect> getElects() {
		return elects;
	}

	public void setElects(List<OaElect> elects) {
		this.elects = elects;
	}

	public void setElectManager(ElectManager electManager) {
		this.electManager = electManager;
	}

	public void setSubjectManager(SubjectManager subjectManager) {
		this.subjectManager = subjectManager;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	public OaContract getOaContract() {
		return oaContract;
	}

	public void setOaContract(OaContract oaContract) {
		this.oaContract = oaContract;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public OaElect getPresentation() {
		return presentation;
	}

	public void setPresentation(OaElect presentation) {
		this.presentation = presentation;
	}

	public List<OaElect> getPelects() {
		return pelects;
	}

	public void setPelects(List<OaElect> pelects) {
		this.pelects = pelects;
	}

}
